using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JobToDdd.Application.JobApp.Dto;
using JobToDdd.Domain.JobInfo;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace JobToDdd.Application.JobApp
{
    public class JobService : ApplicationService, IJobService
    {
        private readonly IRepository<Job> _jobRepo;
        private readonly IRepository<Company> _companyRepo;
        private readonly IRepository<City> _cityRepo;

        public JobService(
            IRepository<Job> jobRepo,
            IRepository<Company> companyRepo,
            IRepository<City> cityRepo)
        {
            _jobRepo = jobRepo;
            _companyRepo = companyRepo;
            _cityRepo = cityRepo;
        }

        public async Task<List<JobDto>> GetJobDtos()
        {
            var jobs = await _jobRepo.GetListAsync();
            var cities = await _cityRepo.GetListAsync();
            var companys = await _companyRepo.GetListAsync();
            jobs.ForEach(job =>
            {
                var city = cities.FirstOrDefault(m => m.Id == job.CityId);
                var company = companys.FirstOrDefault(m => m.Id == job.CompanyId);
                job.GetJob(job, city, company);
            });
            var jobDtos = ObjectMapper.Map<List<Job>, List<JobDto>>(jobs);
            return jobDtos;
        }
    }
}